// import {
// 	getAllowedUrls
// } from "./domain.js"


let token =uni.getStorageSync('token')||''

//POST请求
function requestPOST(url, data) {
	let promise = new Promise(function(resolve, reject) {
		uni.showLoading({
			title: "加载中"
		})
		// console.log(token=="");
		uni.request({
			url,
			data,
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			method: 'POST',
			success: (res) => {
				// console.log(res);
				uni.hideLoading()
				resolve( replaceUrlsInData(res.data))
			},
			fail: (res) => {
				// console.log(res);
				uni.hideLoading()
				reject(res)
			}
		})
	});
	return promise;
}
function requestPOST1(url, data) {
	let promise = new Promise(function(resolve, reject) {
		// console.log(token=="");
		uni.request({
			url,
			data,
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			method: 'POST',
			success: (res) => {
				// console.log(res);
				resolve(replaceUrlsInData(res.data))
			},
			fail: (res) => {
				// console.log(res);
				reject(res)
			}
		})
	});
	return promise;
}


//GET请求
function requestGET(url, params) {
	let promise = new Promise(function(resolve, reject) {
		uni.request({
			url,
			data: params,
			method: 'GET',
			success: (res) => {
				resolve(replaceUrlsInData(res.data))
			},
			fail: (res) => {
				reject(replaceUrlsInData(res.data))
			}
		})
	});
	return promise;
}

//upload
function upload(url, params,folder) {
	let promise = new Promise(function(resolve, reject) {
		uni.showLoading({
			title: "加载中"
		})
		// 上传文件
		uni.uploadFile({
			url,
			filePath: params,
			name: 'file',
			formData: {
				folder: folder
			},
			success: (res) => {
				uni.hideLoading()
				resolve(JSON.parse(replaceUrlsInData(res.data)))
			},
			fail: (res) => {
				uni.hideLoading()
				reject(replaceUrlsInData(res.data))
			}
		})
	});
	return promise
}
let allowedUrls = [
	'https://libc-ddanm.bujufangshi.com/Index/index',
	'https://libc-ddanm.bujufangshi.com/Shop/getList',
	'https://libc-ddanm.bujufangshi.com/Shop/getDetail',
	'https://libc-ddanm.bujufangshi.com/Shop/serviceList',
	'https://libc-ddanm.bujufangshi.com/Shop/getDetail',
	'https://libc-ddanm.bujufangshi.com/Flow/confirmOrder',
	'https://libc-ddanm.bujufangshi.com/OrderInfo/orderList',
	'https://libc-ddanm.bujufangshi.com/OrderInfo/orderDetail',
	'https://libc-ddanm.bujufangshi.com/OrderInfo/evaluatePage'
]

function getAllowedUrls(apiurl, data) {
	if (allowedUrls.includes(apiurl)) {
		// 执行替换操作
		const processedData = replaceUrlsInData(data);
		console.log(processedData,"processedData");
		// 返回处理后的数据（根据实际需求使用）
		return processedData;
	}else{
		console.log(data,"processedData");
		return data;
	}
}

function replaceUrlsInData(data) {
	if (typeof data === 'string') {
		return data.replace(
			/http:\/\/libc-ddanmo-file\.bujufangshi\.com/g,
			'https://libc-ddanmo-file.bujufangshi.com'
		);
	}

	if (Array.isArray(data)) {
		return data.map(item => replaceUrlsInData(item));
	}

	if (typeof data === 'object' && data !== null) {
		const newObj = {};
		for (const key in data) {
			newObj[key] = replaceUrlsInData(data[key]);
		}
		return newObj;
	}

	return data;
}

export default{
	requestPOST,
	requestPOST1,
	requestGET,
	upload
}
